<?php

class Common_DataTable_Analyser
{

    private $dataTable = null;

    public function __construct(Common_DataTable $dataTable)
    {
        $this->setDataTable($dataTable);
    }

    public function setDataTable(Common_DataTable $dataTable)
    {
        $this->dataTable = $dataTable;
    }

    public function getDataTable()
    {
        return $this->dataTable;
    }

    public function countValueFrequenciesForColumns($columns)
    {
        
        $frequencies = array();
        foreach ($columns as $columnKey) {
            $frequencies[$columnKey] = array();
        }

        $data = $this->getDataTable()->toArray();
        foreach ($columns as $columnKey) {

            $column = $this->getDataTable()->getColumn($columnKey);

            foreach ($data as $row) {
                if (isset($row[$columnKey])) {
                    
                    $value = $row[$columnKey];

                    if(isset($column['format'])) {

                        $formatType = $column['format'];

                        if($formatType != 'html') {

                            $formatOptions = isset($column['format_options'])
                                ? $column['format_options']
                                : array();

                            $value = Utility_FormatFunctions::format_value($formatType, $value, $row, $formatOptions);
                        }

                    }
                    
                    if (!isset($frequencies[$columnKey][$value])) {
                        $frequencies[$columnKey][$value] = 0;
                    }

                    $frequencies[$columnKey][$value]++;

                }
            }

        }

        return $frequencies;
        
    }

}
